#!/bin/bash
# USAGE:
#       waitjob hostname.jobid
#	waitjob (hostname.jobid1 hostname.jobid2 ...)
#
# DESCRIPTION:
#  WAITJOB is an internal ADORE command.
#  It loops over the given jobs and returns upon their completion.
#  
#  This script is compatible with the Torque-PBS system.
#
# INPUT:
#  hostname.jobid: hostname of the system running the job and the job number 
#    separated by a dot. This is printed on the screen after each quejob command.
#    Alternatively a list of jobs can be given. In this case this command 
#    will return only after all jobs are finished.
#
# OUTPUT:
#    Displays the number of minutes passed on the screen. No files are generated.
#
# FILES:
#   ${ADORESCR}/fun/waitjob


jobIds=($@)
bold "Waiting for jobs to finish."
echo "Counter displays number of minutes passed."
cntr=0
echo -n "${cntr}m."
for jobId in ${jobIds[*]}
do
  #check each job to see if they are all finished.
  while [ 1 ]
  do
    #wait until last submitted job finishes.
    check -j ${jobId}
    jobStatus=$?
    if [ $jobStatus -eq 0 ]; then
    	echo "Job ${jobId} finished." #qstat reports job as completed.
        break;
    elif [ $jobStatus -eq 134 ]; then
        echo "Job ${jobId} not found."
        break; #Testing this... It means job is not in the job list anymore. Might be long gone.
    elif [ $jobStatus -eq 153 ]; then
        break; #This is apparently normal: http://www.clusterresources.com/pipermail/torqueusers/2005-July/001677.html
    fi
    sleep 60
    cntr=$((${cntr}+1))
    echo -n "${cntr}m."
  done
done
echo "" #print an empty line for the counter. 